package com.idrive.database.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;

import com.idrive.database.DatabaseHelper;
import com.idrive.database.DatabaseUtility;
import com.idrive.database.dto.LocationData;

public class LocationDataDao {
	private SQLiteDatabase database;
	private String[] allColumns = { DatabaseUtility.COLUMN_LOCATION_TIME, DatabaseUtility.COLUMN_LOCATION_LATITUDE,
	        DatabaseUtility.COLUMN_LOCATION_LONGITUDE, DatabaseUtility.COLUMN_LOCATION_GROUP_ID };

	public LocationDataDao(Context context) {
		DatabaseHelper.init(context);
		database = DatabaseHelper.getInstance().getWritableDatabase();
	}

	public void addLocation(LocationData location) {
		ContentValues values = new ContentValues();
		values.put(DatabaseUtility.COLUMN_LOCATION_TIME, location.getTime());
		values.put(DatabaseUtility.COLUMN_LOCATION_LATITUDE, location.getLatitude());
		values.put(DatabaseUtility.COLUMN_LOCATION_LONGITUDE, location.getLongitude());
		values.put(DatabaseUtility.COLUMN_LOCATION_GROUP_ID, location.getGroupId());

		database.insert(DatabaseUtility.TABLE_LOCATION, null, values);

	}

	public void deleteOldRecords() {
		database.delete(DatabaseUtility.TABLE_LOCATION, "date(now) > date('time','+5 days')", null);
	}

	public void clear() {
		database.delete(DatabaseUtility.TABLE_LOCATION, null, null);
	}

	public List<LocationData> getLocation() {
		List<LocationData> list = new ArrayList<LocationData>();
		Cursor cursor = database.query(DatabaseUtility.TABLE_LOCATION, allColumns, null, null, null, null, "ROWID");

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			LocationData location = new LocationData();
			location.setTime(cursor.getLong(cursor.getColumnIndex(DatabaseUtility.COLUMN_LOCATION_TIME)));
			location.setLatitude(cursor.getDouble(cursor.getColumnIndex(DatabaseUtility.COLUMN_LOCATION_LATITUDE)));
			location.setLongitude(cursor.getDouble(cursor.getColumnIndex(DatabaseUtility.COLUMN_LOCATION_LONGITUDE)));
			location.setGroupId(cursor.getString(cursor.getColumnIndex(DatabaseUtility.COLUMN_LOCATION_GROUP_ID)));

			list.add(location);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return list;
	}

	public List<LocationData> getGroupLocation() {
		List<LocationData> list = new ArrayList<LocationData>();
		Cursor cursor = database.query(DatabaseUtility.TABLE_LOCATION,
		        new String[] { DatabaseUtility.COLUMN_LOCATION_GROUP_ID }, null, null,
		        DatabaseUtility.COLUMN_LOCATION_GROUP_ID, null, "ROWID");

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			LocationData location = new LocationData();
			location.setGroupId(cursor.getString(cursor.getColumnIndex(DatabaseUtility.COLUMN_LOCATION_GROUP_ID)));

			list.add(location);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return list;
	}

	public List<LocationData> getLocation(String groupId) {
		List<LocationData> list = new ArrayList<LocationData>();
		Cursor cursor = database.query(DatabaseUtility.TABLE_LOCATION, allColumns,
		        DatabaseUtility.COLUMN_LOCATION_GROUP_ID, new String[] { groupId }, null, null, "ROWID");

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			LocationData location = new LocationData();
			location.setTime(cursor.getLong(cursor.getColumnIndex(DatabaseUtility.COLUMN_LOCATION_TIME)));
			location.setLatitude(cursor.getDouble(cursor.getColumnIndex(DatabaseUtility.COLUMN_LOCATION_LATITUDE)));
			location.setLongitude(cursor.getDouble(cursor.getColumnIndex(DatabaseUtility.COLUMN_LOCATION_LONGITUDE)));
			location.setGroupId(cursor.getString(cursor.getColumnIndex(DatabaseUtility.COLUMN_LOCATION_GROUP_ID)));

			list.add(location);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return list;
	}

	public long getRecordCount() {
		return DatabaseUtils.queryNumEntries(database, DatabaseUtility.TABLE_LOCATION);
	}

}
